﻿/*
·´`·.¸.»------------------------------------------------------------ë·´`·.¸.»
        File: UnsafeBlackOutAlgorithm.cs
        Project: Themaopdracht 7
        Author: Felix Mann

        Description:
        This algorithm fills up a bitmap with black pixels. It is very
        similar to BlackOutAlgorithm except that the pixel access is 
        done in an unsafe fashion thus saving a little of precious CPU time.
·´`·.¸.»------------------------------------------------------------ë·´`·.¸.»
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace THO7_Team8
{
    class DynamicThreshold : VisionAlgorithm
    {
        public DynamicThreshold(String name) : base(name) { }
        public unsafe override System.Drawing.Bitmap DoAlgorithm(System.Drawing.Bitmap sourceImage)
        {
            int hoog = 0;
            int laag = 0;
            BitmapData bd;
            Bitmap myCopy = new Bitmap(sourceImage);
            bd = myCopy.LockBits(new Rectangle(0, 0, myCopy.Width, myCopy.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            int hoogTotaal = 0;
            int laagTotaal = 0;
            unsafe
            {
                byte* pointer = (byte*)bd.Scan0.ToPointer();
                for (int i = 0; i < (bd.Width * bd.Height); i++)
                {
                    if (pointer[0] >= 155 || pointer[1] >= 155 || pointer[2] >= 155)
                    {
                        hoog += 3; //hoog++
                        hoogTotaal = hoogTotaal + pointer[0] + pointer[1] + pointer[2];

                    }
                    else
                    {
                        laag += 3;
                        laagTotaal = laagTotaal + pointer[0] + pointer[1] + pointer[2];
                    }
                    pointer += 3;

                }
                int gem1 = hoogTotaal / hoog;
                myCopy.UnlockBits(bd);
                Threshold((byte)gem1, ref myCopy);
            }
            return myCopy;
        }
        public void Threshold(byte value, ref Bitmap b)
        {
            BitmapData bd;
            bd = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            unsafe
            {
                byte* pointer = (byte*)bd.Scan0.ToPointer();
                for (int i = 0; i < (bd.Width * bd.Height); i++)
                {
                    if (pointer[0] <= value && pointer[1] <= value && pointer[2] <= value)
                    {
                        pointer[0] = 0;
                        pointer[1] = 0;
                        pointer[2] = 0;
                    }
                    else
                    {
                        pointer[0] = 255;
                        pointer[1] = 255;
                        pointer[2] = 255;
                    }
                    pointer += 3;
                }
            }
            b.UnlockBits(bd);
        }
    }
}
